<script setup lang="ts">
import {CircleCheckFilled} from '@element-plus/icons-vue';
import {onMounted, ref, watchEffect} from 'vue';
import {countDown} from "@/utils/countdown/index";
import {getOrderDetailApi} from '@/api/order';
import {useRoute, useRouter} from "vue-router";
import {ElMessage} from "element-plus";
//使用路由
let route = useRoute()
let router = useRouter()
//获取提交订单的相关信息
//订单详情
const orderDetail: any = ref({})
//倒计时函数引用
let countDownTimer = countDown(() => {
  ElMessage.error('订单已超时，请重新提交订单')
  router.push('/')
})


// 获取订单详情
async function getOrderDetail() {
  let res = await getOrderDetailApi(route.params.orderId)
  if (res.msg == '操作成功') {
    orderDetail.value = res.result
    let timeStamp = getTimeStamp(orderDetail.value.createTime, orderDetail.value.payLatestTime)
    // 倒计时
    countDownTimer.startCountDown(timeStamp)
  }
}

//订单跳转
const baseUrl = 'http://pcapi-xiaotuxian-front-devtest.itheima.net'
const backUrl = 'http://127.0.0.1:5173/payBack'
const redirectUrl = encodeURIComponent(backUrl)
const payUrl = `${baseUrl}/pay/aliPay?orderId=${route.params.orderId}&redirect=${redirectUrl}`

// 获取时间戳
function getTimeStamp(startTime: string, endTime: string) {
  const start: any = new Date(startTime);
  const end: any = new Date(endTime);
  // 计算时间差（毫秒）
  const timeDiff = end - start;
  // 将时间差转换为总秒数
  let totalSeconds = Math.floor(timeDiff / 1000);

  return totalSeconds;
}


onMounted(() => {
  getOrderDetail()
})

</script>

<template>
  <div class="pay">
    <div class="pay-heart">
      <div class="pay-info">
        <div class="pay-info-left">
          <div class="pay-info-left-icon">
            <el-icon>
              <CircleCheckFilled/>
            </el-icon>
          </div>
          <div class="pay-info-left-text">
            <p>订单提交成功！请尽快完成支付。</p>
            <span>支付还剩 {{ countDownTimer.formatTime }} 小时, 超时后将取消订单</span>
          </div>
        </div>
        <div class="pay-info-right">
          <p>应付金额：<span>¥ {{ orderDetail.payMoney }}</span></p>
        </div>
      </div>
      <div class="pay-type">
        <div class="address-title">
          <h3> 选择以下支付方式付款 (仅用于教学演示) </h3>
        </div>
        <div class="pay-type-item">
          <div class="pay-type-item-title">
            <span>支付平台</span>
          </div>
          <div class="pay-type-item-type">
            <a href="javascript:;">微信支付</a>
            <a :href="payUrl">支付宝</a>
          </div>
        </div>
        <div class="pay-type-item">
          <div class="pay-type-item-title">
            <span>支付方式</span>
          </div>
          <div class="pay-type-item-type">
            <a href="javascript:;">招商银行</a>
            <a href="javascript:;">工商银行</a>
            <a href="javascript:;">农业银行</a>
            <a href="javascript:;">建设银行</a>
            <a href="javascript:;">邮政储蓄银行</a>
            <a href="javascript:;">交通银行</a>
            <a href="javascript:;">中信银行</a>
            <a href="javascript:;">浦发银行</a>
            <a href="javascript:;">民生银行</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.pay {
  width: 100%;
  background-color: #f5f5f5;
  padding: 1.667rem 0;
  box-sizing: border-box;
}

.pay-heart {
  width: var(--primary-heart-width);
  margin: 0 auto;
  padding: 0 6.667rem;
}

.pay-info {
  background-color: #fff;
  height: 20rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 6.667rem;
  box-sizing: border-box;
}

.pay-info-left {
  display: flex;
  gap: 0.833rem;
}

.pay-info-left-icon {
  font-size: 6.667rem;
  color: var(--primary-color);
}

.pay-info-left-text {
  margin-top: 1.667rem;
}

.pay-info-left-text p {
  font-size: 1.667rem;
  color: #333;
}

.pay-info-left-text span {
  font-size: 1.333rem;
  color: #999;
}

.pay-info-right p {
  font-size: 1.33rem;
  color: #999;
}

.pay-info-right p span {
  font-size: 1.667rem;
  color: #cf4444;
}

.pay-type {
  margin-top: 1.667rem;
  background-color: #fff;
  padding: 1.667rem 1.667rem 5.833rem 1.667rem;
  box-sizing: border-box;
}

.address-title {
  padding-bottom: 1.66667rem;
  border-bottom: 2px solid #f5f5f5;
}

.address-title h3 {
  font-size: 1.33333rem;
  color: #333;
  font-weight: normal;
}

.pay-type-item-title {
  font-size: 1.33333rem;
  color: #333;
}

.pay-type-item {
  margin-top: 1.667rem;
}

.pay-type-item-type {
  display: flex;
  margin-top: 1.667rem;
}

.pay-type-item-type a {
  display: inline-block;
  width: 12.5rem;
  height: 50px;
  text-align: center;
  line-height: 50px;
  font-size: 1.167rem;
  color: #666;
  border: 1px solid #c4c4c4;
  border-radius: 0.5rem;
  margin-right: 1.667rem;
}

.pay-type-item-type a:hover {
  border: 1px solid var(--primary-color);
}
</style>